# Define the paths
$sourceFilePath = "20241109.txt" # The file containing the list of file names
$searchDirectory = "C:\Path\To\SearchDirectory" # Directory to search files
$destinationDirectory = "20241109" # Destination directory
$outputFilePath = "F20241109.txt" # Output file to write the lines

# Initialize the output file
Set-Content -Path $outputFilePath -Value "" # Clear or create the output file


# Create the destination directory if it doesn't exist
if (!(Test-Path -Path $destinationDirectory)) {
    New-Item -ItemType Directory -Path $destinationDirectory
}

# Read the file names from the source file
$fileNames = Get-Content -Path $sourceFilePath

# Loop through each file name and find the file in the directory
foreach ($fileName in $fileNames) {
    # Trim any whitespace
    $fileName = $fileName.Trim()

    # Find the file in the search directory
    $filePath = Get-ChildItem -Path $searchDirectory -Recurse -File | Where-Object { $_.Name -eq $fileName }

    if ($filePath) {
        # Copy the file to the destination directory
        Copy-Item -Path $filePath.FullName -Destination $destinationDirectory -Force
        Write-Host "Copied: $fileName to $destinationDirectory"
        $line = "xadd FILE01 * msg 00 $($fileName)"
        Add-Content -Path $outputFilePath -Value $line
        Write-Host "Added to output: $line"
    } else {
        Write-Host "File not found: $fileName"
    }
}